package com.example.lottery_system.service.mq;


import com.example.lottery_system.common.utils.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

import static com.example.lottery_system.common.config.DirectRabbitConfig.*;

@Component
@RabbitListener(queues = DLX_QUEUE_NAME)
public class DlxMqReceiver {
    Logger logger = LoggerFactory.getLogger(DlxMqReceiver.class);

    @Autowired
    RabbitTemplate rabbitTemplate;
    @RabbitHandler
    public void process(Map<String,String> message) {
        logger.info("死信队列接收到参数dlxQueue:{}" + JacksonUtil.writeValueAsString(message));
        //        校验数据
        if (CollectionUtils.isEmpty(message)) {
            logger.warn("dlxProcess::dlxQueue为空!");
            return;
        }

        // 重新加入到普通队列中， 重新执行相关逻辑
        rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING,message);
//        进行重发操作
    }
}
